package com.zzt.service;

import com.zzt.dao.MemberinfoMapper;
import com.zzt.entity.MemberAndMessage;
import com.zzt.entity.Memberinfo;
import com.zzt.entity.MemberinfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MemberInfoServiceImpl implements MemberInfoService{
    @Resource
    MemberinfoMapper memberinfoMapper;

    @Override
    public MemberAndMessage registerMember(Memberinfo memberinfo) {
        //先查一下会不会重名
        MemberinfoExample example = new MemberinfoExample();
        MemberinfoExample.Criteria criteria = example.createCriteria();
        criteria.andNicknameEqualTo(memberinfo.getNickname());
        List<Memberinfo> memberinfos = memberinfoMapper.selectByExample(example);
        if (memberinfos.size()>0){
            return new MemberAndMessage(null,2,"用户名已存在");
        }

        //如果这个用户名还未被注册
        //先加密
        memberinfo.setPwd(new BCryptPasswordEncoder().encode(memberinfo.getPwd()));
        memberinfoMapper.insert(memberinfo);
        return new MemberAndMessage(memberinfo,1,"注册成功！正在前往登录...");
    }

    @Override
    public MemberAndMessage loginMember(String nickname, String pwd) {
        MemberinfoExample example = new MemberinfoExample();
        MemberinfoExample.Criteria criteria = example.createCriteria();

        //不知道出了什么毛病 直接赋值nickname查不到东西，直接写死如"abc"又能查到
        criteria.andNicknameEqualTo(nickname);
        List<Memberinfo> memberinfos = memberinfoMapper.selectByExample(example);

        if(memberinfos.size()<1){     //用户名不存在
            return new MemberAndMessage(null,200,"用户名或密码不正确");
        }
        for (Memberinfo memberinfo : memberinfos) {
            // ！！踩坑：这里不能用==  因为是字符串
//            if(memberinfo.getPwd().equals(new BCryptPasswordEncoder().encode(pwd))){

            //用加密来验证
            if(new BCryptPasswordEncoder().matches(pwd,memberinfo.getPwd())){
                return new MemberAndMessage(memberinfo,400,"登录成功，即将跳转到主页面...");
            }else {
                return new MemberAndMessage(null,200,"用户名或密码不正确");
            }
        }
        return new MemberAndMessage(null,200,"发生未知错误");
    }

    @Override
    public int updatePersonInfo(Memberinfo memberinfo,String oldpwd) {
        //防止出现 没改用户名 却被判定用户名已存在不给修改的情况
        //先查一下这个用户名是否跟*别人*的一样（要记得不是本身mno，是别人的mno
        MemberinfoExample example = new MemberinfoExample();
        MemberinfoExample.Criteria criteria = example.createCriteria();
        //用户名相同
//        criteria.andnotlike
        criteria.andNicknameEqualTo(memberinfo.getNickname());
        //mno不相同： 也就是不是自己
        criteria.andMnoNotEqualTo(memberinfo.getMno());
        List<Memberinfo> memberinfos = memberinfoMapper.selectByExample(example);
        if(memberinfos.size()>0){   //如果已经有这个名字了
            return -1;
        }

        //用户名还未被取的话就修改
        System.out.println("新密码"+memberinfo.getPwd());
        System.out.println("旧密码"+oldpwd);
        if(!oldpwd.equals(memberinfo.getPwd())){    //如果密码不一样了 加密新密码然后修改
            memberinfo.setPwd(new BCryptPasswordEncoder().encode(memberinfo.getPwd()));
        }
        //如果密码还是原来的，那么久不用再给密码加密了，也不修改密码
        int i = memberinfoMapper.updateByPrimaryKey(memberinfo);
        //1: 修改成功
        return i;
    }

    @Override
    public MemberAndMessage showMemberInfo(int mno) {
        Memberinfo memberinfo = memberinfoMapper.selectByPrimaryKey(mno);
        return new MemberAndMessage(memberinfo,400,"查找信息成功");


    }

}
